namespace SharpTalk.Core.Base
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;
    using Extensions;
    using Interfaces;
    using Diagnostic;
    using System.Runtime.Serialization;
    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    [Serializable]
    public class PropertyChangedBase 
        : Traceable, INotifyPropertyChangedEx
    {
        private bool _isNotifying = true;
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        [field: NonSerialized]
        [field: IgnoreDataMember]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Create a new instance of the <see cref="T:SharpTalk.Core.Base.PropertyChangedBase"/> class.
        /// </summary>
        public PropertyChangedBase()
        {
        }

        /// <summary>
        /// Enables/Disables property change notification.
        /// </summary>
        [DataMember]
        public bool IsNotifying
        {
            get { return _isNotifying; }
            set { _isNotifying = value; }
        }
        /// <summary>
        /// Notifies subscribers of the property change.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        public void NotifyPropertyChange(string propertyName)
        {
            if(IsNotifying)
                OnPropertyChanged(propertyName);
        }
        /// <summary>
        /// Notifies subscribers of the property change.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="property">The property expression. Ex: ()=>Name</param>
        public virtual void NotifyPropertyChange<TProperty>(Expression<Func<TProperty>> property)
        {
            if (property != null)
                NotifyPropertyChange(property.GetMemberInfo().Name);
        }
        /// <summary>
        /// Notifies subscribers of the property change.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <typeparam name="TDependentProperty">The type of the dependent property.</typeparam>
        /// <param name="property">The property expression.</param>
        /// <param name="dependentProperty">The dependent property expression.</param>
        public virtual void NotifyPropertyChange<TProperty, TDependentProperty>(Expression<Func<TProperty>> property, Expression<Func<TDependentProperty>> dependentProperty)
        {
            if(property !=null)
                NotifyPropertyChange(property.GetMemberInfo().Name);
            if(dependentProperty!=null)
                NotifyPropertyChange(dependentProperty.GetMemberInfo().Name);
        }
        /// <summary>
        /// Notifies subscribers of the property change.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <typeparam name="TDepepentProperty">The type of the dependent property.</typeparam>
        /// <param name="property">The property expression.</param>
        /// <param name="dependentProperty">The dependent property expression.</param>
        /// <param name="condition">Condition to notify dependent property</param>
        public virtual void NotifyPropertyChange<TProperty, TDepepentProperty>(Expression<Func<TProperty>> property, Expression<Func<TDepepentProperty>> dependentProperty, Func<bool> condition)
        {
            if (property != null)
                NotifyPropertyChange(property.GetMemberInfo().Name);
            if(dependentProperty!=null && condition())
                NotifyPropertyChange(dependentProperty.GetMemberInfo().Name);
        }
        /// <summary>
        /// Raises a change notification indicating that all bindings should be refreshed.
        /// </summary>
        public void Refresh()
        {
            NotifyPropertyChange(String.Empty);
        }
        /// <summary>
        /// Raise the PropertyChanged event
        /// </summary>
        /// <param name="property">Property name</param>
        private void OnPropertyChanged(string property)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(property));
        }
    }
}